<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/11/23
 * Time: 13:25
 */
namespace hotel\modules\apiadmin\controllers;

use hotel\common\models\Activity;
use hotel\common\models\ActivityDate;
use hotel\common\models\ActivityLog;
use hotel\common\utils\Tool;
use \Yii;

class ActivityController extends CoreController
{


    /**
     * @return string
     */
    public function actionActivityList()
    {
        $post = Yii::$app->request->post();
        $res = Activity::lists($post);
        return Tool::showRes([0, '获取成功'], $res);
    }

    /**
     * @return string
     */
    public function actionAddActivity()
    {
        $post = Yii::$app->request->post();
        Activity::setData($post);
        return Tool::showRes([0, '操作成功']);
    }

    /**
     * @return string
     */
    public function actionUpdateActivity()
    {

        $post = Yii::$app->request->post();
        Activity::updateData($post);
        return Tool::showRes([0, '操作成功']);
    }


    /**
     * @return string
     */
    public function actionDelActivity()
    {
        $post = Yii::$app->request->post();
        $ids = $post['ids'];
        if (empty($ids)) return Tool::showRes([400, '参数有误']);
        $ids_arr = explode(',', $ids);
        if (!$res = Activity::deleteAll(['in', 'id', $ids_arr])) {
            return Tool::showRes([400, '删除失败']);
        } else {
            return Tool::showRes([0, '删除成功']);
        }
    }

    //活动日期设置
    public function actionSetDate(){
        $date_arr = $this->request('date');
        $type = $this->request('type');
        $activity_id = $this->request('activity_id');
        if(empty($date_arr) || empty($type) || empty($activity_id)){
            return Tool::showRes([400, '参数有误']);
        }
        if($type == 1){
            $arr = [];
            foreach($date_arr as $k=>$v){
                $arr[$k][] = $activity_id;
                $arr[$k][] = $v;
                $arr[$k][] = date('Y-m-d H:i:s');
            }
            $res= \Yii::$app->db->createCommand()->batchInsert(ActivityDate::tableName(), ['activity_id','date','create_time'], $arr)->execute();
            if($res){
                return Tool::showRes([0, '操作成功']);
            }
            return Tool::showRes([400, '操作失败']);
        }else{
            if (false !== $res = ActivityDate::deleteAll(['in', 'date', $date_arr])) {
                return Tool::showRes([0, '操作成功']);
            } else {
                return Tool::showRes([400, '操作失败']);
            }
        }
    }

    //获取活动日期设置
    public function actionGetDate(){
        $activity_id = $this->request('activity_id');
        $data = Activity::find()
            ->with(['activity'])
            ->where("id = $activity_id")
            ->asArray()->all();
        return Tool::showRes([0, '获取活动信息成功'],$data);
    }


    //活动报名列表
    public function actionGetActivityLog(){
        $activity_id = $this->request('activity_id');
        $page_size = $this->request('page_size',10);
        $page = $this->request('page',1);
        $list = ActivityLog::find()->where("activity_id = $activity_id")->offset($page_size*($page-1))->limit($page_size)->asArray()->all();
        $counts = ActivityLog::find()->where("activity_id = $activity_id")->count();
        $data['list'] = $list;
        $data['pageInfo'] = [
            'count' => $counts,
            'pageSize' => $page_size,
            'currPage' => $page,
            'tatalPage' => ceil($counts/$page_size),
        ];
        return Tool::showRes([0, '获取列表成功'],$data);
    }

    //修改活动报名状态
    public function actionEditActivityLog(){
        $id = $this->request('id');
        if(empty($id)){
            return Tool::showRes([400, 'id不能为空']);
        }
        $model = new ActivityLog();
        $model = $model::findOne($id);
        $model->order_state = $this->request('order_state');
        if($model->save()) {
            return Tool::showRes([0, '操作成功']);
        }
        return Tool::showRes([400, '操作失败']);
    }

}